package cn.zhz.service;

import cn.zhz.dao.EquipmentDao;
import cn.zhz.pojo.Equipment;
import cn.zhz.pojo.EquipmentCondition;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.List;

public class EquipmentService {
    private EquipmentDao eDao = new EquipmentDao();

    public void addNewEquipment(Equipment equipment){
        Transaction tx =null;

        try {
            tx=eDao.currentSession().beginTransaction();
            eDao.save(equipment);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
    }

    public void updateEquipment(Equipment equipment){
        Transaction tx = null;

        try {
            tx=eDao.currentSession().beginTransaction();
            Equipment equipmentToUpdate = eDao.load(equipment.getId());
            equipmentToUpdate.setTypeId(equipment.getTypeId());
            equipmentToUpdate.setPriceFactory(equipment.getPriceFactory());
            equipmentToUpdate.setDateFactory(equipment.getDateFactory());
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
    }
    public void deleteEquipment(int id){
        Transaction tx = null;
        try {
            tx=eDao.currentSession().beginTransaction();
            Equipment equipmentToDelete = eDao.load(id);
            eDao.delete(equipmentToDelete);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
    }

    public List<Equipment> findByTypeId(Equipment condition){
        Transaction tx = null;
        List<Equipment> equipmentList = null;
        try {
            tx=eDao.currentSession().beginTransaction();
            equipmentList = eDao.findByTypeId(condition);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
        return equipmentList;
    }
    public List<Equipment> findByPrice(Equipment condition){
        Transaction tx = null;
        List<Equipment> equipmentList = null;
        try {
            tx=eDao.currentSession().beginTransaction();
            equipmentList = eDao.findByPrice(condition);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
        return equipmentList;
    }
    public List<Equipment> findByDate(EquipmentCondition condition){
        Transaction tx =null;
        List<Equipment> equipmentList = null;
        try {
            tx=eDao.currentSession().beginTransaction();
            equipmentList=eDao.findByDate(condition);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
        return equipmentList;
    }

    public List<Equipment> findEquipmentByCondition(EquipmentCondition condition){
        Transaction tx = null;
        List<Equipment> equipmentList = null;
        try {
            tx=eDao.currentSession().beginTransaction();
            StringBuilder hql = new StringBuilder("from Equipment as equipment where 1=1 ");
            if(condition.getTypeId()!=null&&condition.getTypeId().length()>0){
                hql.append(" and equipment.typeId like :typeId");
            }
            if(condition.getPriceMin()!=0){
                hql.append(" and equipment.priceFactory > :priceMin");
            }
            if( condition.getPriceMax()!=0){
                hql.append(" and equipment.priceFactory < :priceMax");
            }
            if(condition.getDateStart()!=null){
                hql.append(" and equipment.dateFactory > :dateStart");
            }
            if(condition.getDateEnd()!=null){
                hql.append(" and equipment.dateFactory < :dateEnd");
            }
            equipmentList = new EquipmentDao().findEquipmentByCondition(hql.toString(),condition);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
        return equipmentList;
    }
}
